package main


//https://studygolang.com/articles/10755
//https://blog.csdn.net/daily886/article/details/103432917
//https://www.cnblogs.com/gobuild/p/5985887.html
//https://blog.csdn.net/m0_47202518/article/details/115035029 布局
//https://codechina.csdn.net/mirrors/lxn/walk/-/blob/master/examples/tableview/tableview.go  表格
//https://www.jianshu.com/p/bbe8ba2e925a 托盘菜单
// https://blog.csdn.net/qq_27517377/article/details/112373536  编译添加图标命令
//rsrc -manifest test.manifest -ico ./ico/log.ico -o rsrc.syso 编译添加图标命令
//打包隐藏cmd 窗口
//https://blog.csdn.net/qq_32394351/article/details/93468119?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EOPENSEARCH%7Edefault-10.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EOPENSEARCH%7Edefault-10.no_search_link
//go build  -ldflags="-s -w -H windowsgui"
// UPX 压缩工具下载
//https://github.com/upx/upx/releases/tag/v3.96
import (
	"fmt"
	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

func main() {
	initLog()
	start()
	log.Println("启动成功")
}

func initWalk(mv MyMainWindow)  {
	icon, err := walk.Resources.Icon("/ico/log.ico")
	if err != nil {
		log.Fatal(err)
	}
	ni, err := walk.NewNotifyIcon(mv)
	if err != nil {
		log.Fatal(err)
	}
	defer ni.Dispose()
	if err := ni.SetIcon(icon); err != nil {
		log.Fatal(err)
	}
	//鼠标在icon上悬浮的信息
	if err := ni.SetToolTip("一个无聊的小程序."); err != nil {
		log.Fatal(err)
	}
}
func initLog()  {
	logFile, err := os.OpenFile("./killPort.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println("open log file failed, err:", err)
		return
	}
	log.SetOutput(logFile)
	log.SetFlags(log.Llongfile | log.Lmicroseconds | log.Ldate)
}
func execPath() (string, error) {
	file, err := exec.LookPath(os.Args[0])
	if err != nil {
		return "", err
	}
	re, err := filepath.Abs(file)
	if err != nil {
		log.Println("The eacePath failed: %s\n", err.Error())
	}
	log.Println("The path is ", re)
	return filepath.Abs(file)
}

func getPath(s string) string {
	re,_ :=execPath()
	p := strings.Replace(re,".exe","",-1)
	i := strings.LastIndex(p,"\\")
	p = string(p[i+1:len(p)])
	icon :=p+s
	return icon
}

func start() {
	// re,_ :=execPath()
	// p := strings.Replace(re,".exe","",-1)
	//icon :=p+"\\ico\\log.png"
	icon :="\\ico\\log.png"
	//icon :=getPath("\\ico\\log.png")
	//log.Println("当前图片路径",icon)
	mv := MyMainWindow{}
	model := NewFooModel()
	var keywords *walk.LineEdit
	var tv *walk.TableView
    if err := (MainWindow{
		Title:    "kill-port",
		MaxSize:  Size{500, 400},
		Layout:   VBox{},
		Icon:icon,
		AssignTo: &mv.MainWindow,
		Children: []Widget{
			Composite{
				MaxSize: Size{0, 50},
				Layout:  HBox{},
				Children: []Widget{
					Label{Text: "端口号: "},
					LineEdit{
						AssignTo: &keywords,
						Text:     "",
					},
					PushButton{
						Text: "查询",
						OnClicked: func() {
							fmt.Println(keywords.Text())
							port := keywords.Text()
							port = strings.Replace(port, " ", "", -1)
							if len(port) == 0 {
								mv.aboutAction_Triggered("请输入查询端口号")
								keywords.SetText("")
								return
							}
							openFooModel(model,port,mv)
							if len(model.items) == 0 {
								mv.aboutAction_Triggered("未查询到数据")
							}
						},
					},
					PushButton{
						Text: "释放端口",
						OnClicked: func() {
							if len(model.items) == 0 {
								mv.aboutAction_Triggered("无数据")
							}
							emptyTable(model)
						},
					},
				},
			},
			Composite{
				//布局
				Layout: HBox{SpacingZero: true},
				Children: []Widget{
					TableView{
						AssignTo:         &tv,
						AlternatingRowBG: true,
						CheckBoxes:       true,
						ColumnsOrderable: true,
						MultiSelection:   true,
						Model:            model,
						Columns: []TableViewColumn{
							{Title: "序号"},
							{Title: "协议"},
							{Title: "本地地址"},
							{Title: "外部地址"},
							{Title: "状态"},
							{Title: "PID"},
							{Title: "服务名字"},
							{Title: "端口"},
							{Title: "内存使用"},
						},
					},
				},
			},
		},
	}.Create());err !=nil{
		log.Fatal(err)
	}
	//initWalk(mv)
	mv.Run()

}

type MyMainWindow struct {
	*walk.MainWindow
}

func (mw *MyMainWindow) aboutAction_Triggered(s string) {
	walk.MsgBox(mw, "提示", s, walk.MsgBoxIconQuestion)
}




